package com.aggron.concurrency.sycncontainer;

import com.aggron.concurrency.annotations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @author LR2SD
 * @description
 * @date 2018-06-11 19:39
 */
@Slf4j
@ThreadSafe
public class VectorExample2 {

    public static void main(String [] args){
        Vector<Integer> vector=new Vector<>();
        //vector.add(1);
        //vector.add(2);
        vector.add(3);
        //test1(vector);
        test2(vector);
        //test3(vector);
    }
    //success
    private static void test3(Vector<Integer> vector) {//for
        for (int i = 0; i < vector.size(); i++) {
            if(vector.get(i).equals(3)){
                vector.remove(i);
            }
        }
    }
    //Exception in thread "main" java.util.ConcurrentModificationException
    private static void test2(Vector<Integer> vector) {//Iterator
        Iterator<Integer> iterator=vector.iterator();
        while (iterator.hasNext()){
            Integer integer=iterator.next();
            if(integer.equals(3)){
                vector.remove(integer);
            }
        }
    }
    //java.util.ConcurrentModificationException
    private static void test1(Vector<Integer> vector) {//foreach
        for(Integer i:vector){
            if(i.equals(3)){
                vector.remove(i);
            }
        }
    }
}
